package com.song.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.pojo.Notice;
import com.song.service.NoticeService;
import com.song.utils.DataGridViewResult;
import com.song.utils.SystemConstant;
import com.song.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2021/5/30 - the current system date.
 * 9:48 - the current system time.
 */
@RestController
@RequestMapping("/admin/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 公告列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(NoticeVo noticeVo){
        //设置分页信息
        PageHelper.startPage(noticeVo.getPage(),noticeVo.getLimit());

        //调用查询方法
        List<Notice> noticeList = noticeService.findNoticeList(noticeVo);

        //创建分页对象
        PageInfo<Notice> pageInfo = new PageInfo<Notice>(noticeList);

        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    //添加公告信息
    @RequestMapping("/addNotice")
    public String addNotice(Notice notice){
        //创建map存放信息
        Map<String,Object> map = new HashMap<String, Object>();
        int addNotice = noticeService.addNotice(notice);
        if(addNotice>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        //map中存放的信息已json形式返回
        return JSON.toJSONString(map);
    }

    //修改公告信息
    @RequestMapping("/updateNotice")
    public String updateNotice(Notice notice){
        Map<String,Object> map = new HashMap<String, Object>();
        int updateNotice = noticeService.updateNotice(notice);
        if(updateNotice>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        //map中存放的信息已json形式返回
        return JSON.toJSONString(map);
    }

    //删除公告信息
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        Map<String,Object> map = new HashMap<String, Object>();
        int deleteById = noticeService.deleteById(id);
        if (deleteById>0){
            map.put(SystemConstant.SUCCESS,true);//删除成功
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);//删除失败
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    //查询所有类型
    @RequestMapping("/noticeList")
    public String deptList(){
        return JSON.toJSONString(noticeService.findAllNoticeList());
    }
}
